<?php

/**
 * Задание ЧПУ на сайте
 */
class UrlManager extends CActiveRecord
{
	public function tableName()
	{
		return 'url_manager';
	}

	public function rules()
	{

		return array(
			array('url, real_url, title, h1, keywords, description', 'required'),
			array('url, real_url, title, h1, keywords, description', 'length', 'max'=>256),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, url, real_url, title, h1, keywords, description', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'url' => 'ЧПУ url',
			'real_url' => 'Реальный url',
			'title' => 'Title',
			'h1' => 'H1',
			'keywords' => 'Keywords',
			'description' => 'Description',
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('url',$this->url, true);
		$criteria->compare('real_url',$this->real_url,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('h1',$this->h1,true);
		$criteria->compare('keywords',$this->keywords,true);
		$criteria->compare('description',$this->description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function findByUrl($url) {
		$criteria = new CDbCriteria;
		
		$criteria->compare('url',$url);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function findByRealUrl($url) {
		$criteria = new CDbCriteria;
	
		$criteria->compare('real_url',$url);
	
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
